package cc.magicjson.rest.jpa;


import cc.magicjson.entity.User;
import cc.magicjson.service.jpa.UserServiceJPA;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/jpa/users")
public class UserControllerJPA {

    private final UserServiceJPA userServiceJPA;

    public UserControllerJPA(UserServiceJPA userServiceJPA) {
        this.userServiceJPA = userServiceJPA;
    }

    @PostMapping
    public String createUser(@RequestBody User user) {
        userServiceJPA.createUser(user);
        return "User created successfully (JPA)";
    }

    @GetMapping("/{id}")
    public User getUser(@PathVariable Long id) {
        return userServiceJPA.getUserById(id);
    }

    @GetMapping
    public List<User> getAllUsers() {
        return userServiceJPA.getAllUsers();
    }

    @PutMapping("/{id}")
    public String updateUser(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        userServiceJPA.updateUser(user);
        return "User updated successfully (JPA)";
    }

    @DeleteMapping("/{id}")
    public String deleteUser(@PathVariable Long id) {
        userServiceJPA.deleteUser(id);
        return "User deleted successfully (JPA)";
    }
}
